<?php
namespace app\models;

/**
 *
 *装箱信息表
 **/
use yii;
use yii\db\Query;

class ContainerModel extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'container';
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }

        if (!empty($where['number'])) {
            $db->andWhere(['number' => $where['number']]);
        }

        return $db->one();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);

        if (!empty($where['date'])) {
            $db->andWhere(['date' => $where['date']]);
        }

        if (!empty($where['number'])) {
            $db->andWhere(['number' => $where['number']]);
        }

        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }

        if (!empty($where['status'])) {
            $db->andWhere(['status' => $where['status']]);
        }

        if ($offset) {
            $db->offset($offset);
        }

        if ($limit) {
            $db->limit($limit);
        }

        if ($order) {
            $db->orderBy($order);
        }

        $list = $db->all();
        return $list;
    }

    public function size($where)
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);

        if (!empty($where['date'])) {
            $db->andWhere(['date' => $where['date']]);
        }

        if (!empty($where['number'])) {
            $db->andWhere(['number' => $where['number']]);
        }

        if (!empty($where['invoice'])) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }

        if (!empty($where['status'])) {
            $db->andWhere(['status' => $where['status']]);
        }

        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    /**
     *获取装箱编号
     */
    public function getContainerNumByParam($field,$where = [])
    {
        $db = new Query();
        $db->select($field)->from($this->table);
        if (!empty($where['status'])) {
        	$db->andWhere(['status'=>$where['status']]);
        }
        $db->orderBy(['create_at'=>SORT_DESC]);
        return $db->all();
    }
}
